package com.ygqh.baby.service.impl;

import com.wdt.api.WdtClient;
import com.wdt.api.WdtQueryTemplate;
import com.wdt.api.WdtResponse;
import com.wdt.api.exception.WdtException;
import com.wdt.api.request.StockQueryRequest;
import com.wdt.api.request.support.StockQueryModel;
import com.wdt.api.response.StockQueryResponse;
import com.wdt.api.response.support.Stock;
import com.ygqh.baby.config.WdtQybConfig;
import com.ygqh.baby.service.WdtQybStockService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/2/27 15:26
 * @since 1.0.0
 */
@Service
public class WdtQybStockServiceImpl implements WdtQybStockService {

    @Autowired
    private WdtClient wdtClient;
    @Autowired
    private WdtQybConfig wdtQybConfig;

    @SuppressWarnings({"unchecked"})
    @Override
    public StockQueryResponse stockQuery(StockQueryRequest queryRequest) throws WdtException {

        return wdtClient.executeQuery(queryRequest, new WdtQueryTemplate() {
            @Override
            public <T extends WdtResponse> T execute(List<T> list) {
                StockQueryResponse res = null;
                for (WdtResponse t : list) {
                    StockQueryResponse response = (StockQueryResponse) t;
                    if (res == null) {
                        res = response;
                    } else {
                        res.getStocks().addAll(response.getStocks());
                    }
                }
                return (T) res;
            }
        });
    }

    @Override
    public StockQueryResponse stockQuery(String skuCode) throws WdtException {
        return stockQuery(skuCode, wdtQybConfig.getWarehouseNo());
    }

    @Override
    public StockQueryResponse stockQuery(String skuCode, String... warehouseNo) throws WdtException {
        StockQueryRequest request = new StockQueryRequest();
        StockQueryModel model = new StockQueryModel();
        model.setSpecNo(skuCode);
        request.setBizModel(model);
        return filterWarehouseNo(stockQuery(request), warehouseNo);
    }

    @Override
    public StockQueryResponse stockQuery(Date startTime, Date endTime, String... warehouseNo) throws WdtException {
        StockQueryRequest request = new StockQueryRequest();
        StockQueryModel model = new StockQueryModel();
        model.setStartTime(startTime);
        model.setEndTime(endTime);
        request.setBizModel(model);
        return filterWarehouseNo(stockQuery(request), warehouseNo);
    }

    @Override
    public StockQueryResponse stockQuery(Date startTime, Date endTime) throws WdtException {
        return stockQuery(startTime, endTime, wdtQybConfig.getWarehouseNo());
    }

    private StockQueryResponse filterWarehouseNo(StockQueryResponse stockQueryResponse, String... warehouseNo) {
        if (warehouseNo.length > 0) {
            List<Stock> stocks = stockQueryResponse.getStocks();
            if (CollectionUtils.isNotEmpty(stocks)) {
                List<String> strings = Arrays.asList(warehouseNo);
                List<Stock> stockList = stocks.stream().filter(stock -> strings.contains(stock.getWarehouseNo())).
                        collect(Collectors.toList());
                stockQueryResponse.setStocks(stockList);
            }
        }
        return stockQueryResponse;
    }
}
